package com.tjc.project.config;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @ClassName RedisTemplateConfig
 * @Author tjc
 * 对redis存储进行序列化
 **/
@AutoConfigureBefore
public class RedisTemplateConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplateConfig(RedisConnectionFactory redisConnectionFactory) {
        // 创建redistribution模板
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        // 链接redis数据库
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 对键值进行序列化指定
        redisTemplate.setKeySerializer(RedisSerializer.string());
        // 对value值进行指定,json数据
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

        return redisTemplate;

    }
}
